#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 14:00:31
# @Description: 电影天堂 https://www.ygdy8.com/index.html
##############################################
'module name'
__author__ = 'DengLibin'

import re

import requests


##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 14:44:08
# @Description: 获取网页内容
##############################################
def get_url_content(url) -> str:
    # 设置请求头
    headers_dic = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
    } 
    resp = requests.get(url, headers=headers_dic)
    # 指定字符集
    resp.encoding = 'gb2312'
    page_content = resp.text
    resp.close()
    return page_content


##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 14:42:08
# @Description: 获取电影的下载地址
##############################################
# @param url 电影详情url
def get_movie_magnet(url):
    page_content = get_url_content(url)
    obj = re.compile(r'<a target="_blank" href="magnet:(?P<url>.*?)>')
    r = obj.finditer(page_content)
    for i in r:
        return "magnet:" + i.group('url')


##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 15:01:18
# @Description: 获取电影和下载地址
##############################################
def run():
    url =  "https://www.ygdy8.com/index.html"
    page_content = get_url_content(url)
  
    obj = re.compile(r"2022新片精品.*?<ul>(?P<ul>.*?)</ul>", re.S)
    obj2 = re.compile(r'最新电影下载.*?href=(?P<url>.*?)>(?P<name>.*?)</a>', re.S)
    r = obj.finditer(page_content)
    for it in r:
        ul = it.group('ul')
        print(ul)
        print('--------------------------------------------------------------')
        r2 = obj2.finditer(ul)
        for li in r2:
            # print(li.group('name'), li.group('url'))
            movie_detail_url = 'https://www.ygdy8.com' + li.group('url').strip("'")
            # print(movie_detail_url)
            magnet = get_movie_magnet(movie_detail_url)
            print(li.group('name'), magnet)
            print("-----------------------------------------------------------------------------------------------")

##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 15:02:42
# @Description: 程序入口
##############################################
if __name__ == '__main__':
    run()
